package zj.it.bhne.greendao;

import java.util.List;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;
import org.greenrobot.greendao.query.Query;
import org.greenrobot.greendao.query.QueryBuilder;

import zj.it.bhne.greendao.entity.Books;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "BOOKS".
*/
public class BooksDao extends AbstractDao<Books, Long> {

    public static final String TABLENAME = "BOOKS";

    /**
     * Properties of entity Books.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        public final static Property Sid = new Property(1, Long.class, "sid", false, "SID");
        public final static Property Name = new Property(2, String.class, "name", false, "NAME");
        public final static Property Amount = new Property(3, int.class, "amount", false, "AMOUNT");
    }

    private Query<Books> students_BooksListQuery;

    public BooksDao(DaoConfig config) {
        super(config);
    }
    
    public BooksDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"BOOKS\" (" + //
                "\"_id\" INTEGER PRIMARY KEY ," + // 0: id
                "\"SID\" INTEGER," + // 1: sid
                "\"NAME\" TEXT," + // 2: name
                "\"AMOUNT\" INTEGER NOT NULL );"); // 3: amount
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"BOOKS\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, Books entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        Long sid = entity.getSid();
        if (sid != null) {
            stmt.bindLong(2, sid);
        }
 
        String name = entity.getName();
        if (name != null) {
            stmt.bindString(3, name);
        }
        stmt.bindLong(4, entity.getAmount());
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, Books entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        Long sid = entity.getSid();
        if (sid != null) {
            stmt.bindLong(2, sid);
        }
 
        String name = entity.getName();
        if (name != null) {
            stmt.bindString(3, name);
        }
        stmt.bindLong(4, entity.getAmount());
    }

    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    @Override
    public Books readEntity(Cursor cursor, int offset) {
        Books entity = new Books( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getLong(offset + 1), // sid
            cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // name
            cursor.getInt(offset + 3) // amount
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, Books entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setSid(cursor.isNull(offset + 1) ? null : cursor.getLong(offset + 1));
        entity.setName(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
        entity.setAmount(cursor.getInt(offset + 3));
     }
    
    @Override
    protected final Long updateKeyAfterInsert(Books entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    @Override
    public Long getKey(Books entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(Books entity) {
        return entity.getId() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "booksList" to-many relationship of Students. */
    public List<Books> _queryStudents_BooksList(Long sid) {
        synchronized (this) {
            if (students_BooksListQuery == null) {
                QueryBuilder<Books> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.Sid.eq(null));
                students_BooksListQuery = queryBuilder.build();
            }
        }
        Query<Books> query = students_BooksListQuery.forCurrentThread();
        query.setParameter(0, sid);
        return query.list();
    }

}
